package controller

import (
    "context"
    pb "gitee.com/micro-services/protocol/verify/server"
    log "github.com/sirupsen/logrus"
    "micro.verify.server/ao"
    "micro.verify.server/lib"
    "micro.verify.server/plugins"
)

type EmailController struct{}

func (ec *EmailController) SendEmail(ctx context.Context, r *pb.SendEmailReq, w *pb.SendEmailRsp) error {
    log.Infof("call request:%+v", r)

    if err := ao.SendEmail(r.Receiver); err != nil {
        log.Errorf("send email error:%+v", err)
        return err
    }

    w.RetCode = lib.Success
    w.Msg = lib.Msg(lib.Success)

    plugins.DealWithTracer(ctx, "go.micro.verify.server.SendEmail", r, w)
    return nil
}

func (ec *EmailController) VerifyCode(ctx context.Context, r *pb.VerifyCodeReq, w *pb.VerifyCodeRsp) error {
    log.Infof("call request:%+v", r)

    b, err := ao.VerifyEmailCode(r.Receiver, r.ReceiverCode)
    if err != nil {
        log.Errorf("verify email code error:%+v", err)
        return err
    }

    w.Result = b

    plugins.DealWithTracer(ctx, "go.micro.verify.server.SendEmail", r, w)
    return nil
}
